package javaLearning.动态代理;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author : K k
 * @date : 10:56 2020/8/25
 */
public class MyInvocationHandler implements InvocationHandler {
    private Object target;

    MyInvocationHandler() {
        super();
    }

    MyInvocationHandler(Object target) {
        super();
        this.target = target;
    }

    @Override
    public Object invoke(Object o, Method method, Object[] args) throws Throwable {
        if("getName".equals(method.getName())){
            //System.out.println("++++++before " + method.getName() + "++++++");
            Object result = method.invoke(target, args);
           // System.out.println("++++++after " + method.getName() + "++++++");
            System.out.println(o==target);
            return result;
        }else{
            Object result = method.invoke(target, args);
            return result;
        }
    }


    public static void main(String[] args) {
        UserService userService = new UserServiceImpl();
        InvocationHandler invocationHandler = new MyInvocationHandler(userService);
        //动态代理获取新实例
        UserService userServiceProxy = (UserService) Proxy.newProxyInstance(UserService.class.getClassLoader(),
                userService.getClass().getInterfaces(), invocationHandler);
        System.out.println(userServiceProxy.getName(1));
        System.out.println(userServiceProxy.getAge(1));


        //1.实例对象
        /*UserService userService=new UserServiceImpl();
        //2.创建代理处理类
        MyInvocationHandler handler=new MyInvocationHandler(userService);
        //3.动态代理创建代理对象
        UserService userServiceProxy= (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(),userService.getClass().getInterfaces(),handler);
        //4.调用方法
        userService.getName(1);
        userService.getAge(1);*/

        List<String> list=new ArrayList<>();
        list.add("hello");
        list.add("world");
        list.add("size");
        Iterator<String> iterator = list.iterator();
        System.out.println(list);
        while (iterator.hasNext()) {
            String s = iterator.next();
            iterator.remove();
            System.out.println(s);
        }
        System.out.println(list);

        int i=1;
        i=i++;
        int j=i++;
        int k=i+ ++i * i++;
        System.out.println(i);
        System.out.println(j);
        System.out.println(k);
        System.out.println(i);
    }

}
